const app = getApp();
wx.cloud.init();
const db=wx.cloud.database()
  /**
   * 生命周期函数--监听页面加载
   */
Page({

  /**
   * 页面的初始数据
   */
  data:{
    nickName:'',
    imgUrls:[
      "/imgs/kou.png",
      "/imgs/group.png",
      "/imgs/3.jpg"
    ],
    interval: 5000,
    showView: true,
    scroll: false,
    ownUserInfo: false,
    userInfo:'',
    userInfo1:'',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    information:[]
  },
  // 点击出现导航栏
  display_menu:function(e){
    this.setData({
      showView: (!this.data.showView),
      scroll: true
    })
  },

   /**
   * 滚动页面导航栏变色
   */
  onPageScroll:function (e) {
		if (e.scrollTop > 0) {
			this.setData({
				scroll: true
			});
		} else {
			this.setData({
				scroll: false
			});
		}
	},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let user=wx.getStorageSync('user')
    this.setData({
      userInfo:user,
      nickName:user.nickName
    })
    let that = this
    db.collection('Index_Information').get({
      success:(res)=>{
        
        that.setData({
          information:res.data
        })
        // console.log(that.data.information)
      }
    })
  },

  
//登录
  login(){
    wx.getUserProfile({
      desc:'必须授权后才能继续使用',
      success:res=>{
        let user=res.userInfo
        //设置本地缓存,把用户信息缓存到本地
        wx.setStorageSync('user',user)
        console.log('用户信息',user)
        app.globalData.userInfo=user
        //console.log(app.globalData.userInfo)
        this.setData({
          userInfo:user,
          nickName:user.nickName,
          userInfo1:2
        })
      }
  })
  },

  cunna(){
    var that=this
    this.setData({
      userInfo1:0
    })
    const db=wx.cloud.database()
  //console.log(that.data.nickName)
  db.collection('Users').where({
    nickName:that.data.nickName
  }).get({
    success:(res)=>{
      if(res.data.length>0){
        console.log('123,zhoufu!')
        App.globalData.userInfo = res.data[0]
        wx.setStorageSync('userInfo', res.data[0])
        wx.switchTab({
          url: '/pages/me/me.js',
        })
      }else{
        // console.log('456,mmzc!')
        db.collection('Users').add({
          data:{
            avatar:that.data.userInfo.avatarUrl,
            nickName:that.data.userInfo.nickName,
            chat_list: [],
            signature:'远赴人间惊鸿宴',
            year_of:'',
            dormitory:'',
            gender:'unknown'
          },
          success:(res)=>{
            // console.log(res)
            // console.log('789,sb!')
          }
        })
      }
    }
  })
  },


  loginOut(){
    this.setData({
        userInfo:''
    })
    wx.setStorageSync('user',null)
  },



  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})